package com.codelab.statemachine.task;

import cn.hutool.core.collection.ListUtil;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.support.DefaultStateMachineContext;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class TaskStateMachinePersist implements StateMachinePersist<TaskStates,TaskEvents,String> {

    @Override
    public void write(StateMachineContext<TaskStates, TaskEvents> stateMachineContext, String s) throws Exception {

    }
    public static List<List<Integer>> generate(int numRows) {
        if(numRows==1){
            List<List<Integer>> result = new ArrayList<>();
            result.add(ListUtil.toList(1));
            return result;
        }
        List<List<Integer>> lastList = generate(numRows-1);
        List<Integer> lastRow = lastList.get(lastList.size()-1);
        List<Integer> currentRows = new ArrayList<>();
        for(int j=0;j<numRows;j++){
            currentRows.add(fetchnum(lastRow,j));
        }

        lastList.add(currentRows);
        return lastList;
    }

    private static Integer fetchnum(List<Integer> lastRow, int j) {
        if(j==0 || j==lastRow.size()){
            return 1;
        }
        return lastRow.get(j) + lastRow.get(j-1);
    }

    public static void main(String[] args) {
        System.out.println(generate(31));
    }

    @Override
    public StateMachineContext<TaskStates, TaskEvents> read(String status) throws Exception {
        StateMachineContext<TaskStates,TaskEvents> result = new DefaultStateMachineContext<TaskStates, TaskEvents>(TaskStates.valueOf(status)
        ,null,null,null,null,TaskStateMachineBuilder.MACHINEID);
        return result;
    }
}
